package com.jd.basic.controller;

import com.jd.basic.service.VerifyCodeService;
import com.jd.basic.util.AjaxResult;
import com.jd.user.dto.EmailRegisterDto;
import com.jd.user.dto.PhoneRegisterDto;
import com.jd.user.dto.SendEmailCodeDto;
import com.jd.user.dto.SendMobileCodeDto;
import com.jd.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     *
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String imageCode) throws IOException {
        System.err.println("Controller" + imageCode);
        String base64ImageStr = verifyCodeService.imageCode(imageCode);
        return AjaxResult.me().success(base64ImageStr);

    }

    /**
     * 获取短信验证码
     */
    @PostMapping("/sendMobileCode")
    public AjaxResult sendMobileCode(@RequestBody @Valid SendMobileCodeDto sendMobileCodeDto){
        System.err.println("发送短信Controller" + sendMobileCodeDto);
        verifyCodeService.sendPhone(sendMobileCodeDto);
        return AjaxResult.me().success();
    }
    /**
     * 获取邮箱验证码
     */
    @PostMapping("/sendEmailCode")
    public AjaxResult sendEmailCode(@RequestBody @Valid SendEmailCodeDto sendEmailCodeDto){
        System.err.println("发送邮箱Controller" + sendEmailCodeDto);
        verifyCodeService.sendEmail(sendEmailCodeDto);
        return AjaxResult.me().success();
    }

    /**
     * 手机注册按钮
     * @param phoneRegisterDto
     * @return
     */
    @PostMapping("/phoneRegister")
    public AjaxResult phoneRegister(@RequestBody @Valid PhoneRegisterDto phoneRegisterDto){
        System.err.println("电话注册"+phoneRegisterDto);
        verifyCodeService.phoneRegister(phoneRegisterDto);
        return AjaxResult.me().success();
    }
    /**
     * 邮箱注册按钮
     * @param emailRegisterDto
     * @return
     */
    @PostMapping("/emailRegister")
    public AjaxResult emailRegister(@RequestBody @Valid EmailRegisterDto emailRegisterDto){
        System.err.println("邮箱注册"+emailRegisterDto);
        verifyCodeService.emailRegister(emailRegisterDto);
        return AjaxResult.me().success();
    }

}
